using System.Threading.Tasks;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;

namespace Xms.Abp.Smsing;

public class BackgroundSmsSendingJob : AsyncBackgroundJob<SmsMessage>, ITransientDependency
{
    protected ISmsSender SmsSender { get; }
    protected ICurrentTenant CurrentTenant { get; }

    public BackgroundSmsSendingJob(
        ISmsSender smsSender,
        ICurrentTenant currentTenant)
    {
        SmsSender = smsSender;
        CurrentTenant = currentTenant;
    }

    public async override Task ExecuteAsync(SmsMessage args)
    {
        using (CurrentTenant.Change(args.TenantId))
        {
            await SmsSender.SendAsync(args);
        }
    }
}
